package cn.com.elite;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;

import sfmi.component.chnchannel.chncallcenter.client.FreeInsuranceClient;
import sfmi.component.chnchannel.chncallcenter.dto.enums.ConnectMode;
import sfmi.component.chnchannel.chncallcenter.dto.freeinsurance.FICoupon;
import sfmi.component.chnchannel.chncallcenter.dto.freeinsurance.FIQuotation;
import sfmi.component.chnchannel.chncallcenter.dto.freeinsurance.key.FICouponKey;
import sfmi.component.chnchannel.chncallcenter.dto.freeinsurance.key.FIQuotationKey;

public class findCouponList extends HttpServlet {
	private static final Log log=LogFactory.getLog(findCouponList.class);

	/**
	 * Constructor of the object.
	 */
	public findCouponList() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String returnxml="";
		try{
			String UserId=request.getParameter("UserId");
			log.debug("UserId:"+UserId);
			String CouponNO=request.getParameter("CouponNO");
			log.debug("CouponNO:"+CouponNO);
			String IP=Util.getIp(request);
			log.debug("IP:"+IP);
			FreeInsuranceClient freeInsuranceClient = new FreeInsuranceClient(ConnectMode.RealServer);
			FICouponKey fiQuotationKey = new FICouponKey();
			fiQuotationKey.setIpAddress(IP);
			fiQuotationKey.setCouponNo(CouponNO);
			fiQuotationKey.setUserId(UserId);
		    List<FICoupon> list=freeInsuranceClient.findCouponList(fiQuotationKey);
		    if(list!=null&&list.size()>0){
		    	Document result=Util.getDoc();
	        	Element root=result.addElement("Result");
		    	for(int i=0;i<list.size();i++){
		    		Element fii=root.addElement("fii");
		    		FICoupon fICoupon=list.get(i);
		    		String ClosedYn=fICoupon.getClosedYn();
		    		log.debug("ClosedYn:"+ClosedYn);
		    		fii.addElement("ClosedYn").addText(Util.getString(ClosedYn));
		    		String CouponNo1=fICoupon.getCouponNo();
		    		log.debug("CouponNo1:"+CouponNo1);
		    		fii.addElement("CouponNo").addText(Util.getString(CouponNo1));
		    		String UsedYn=fICoupon.getUsedYn();
		    		log.debug("UsedYn:"+UsedYn);
		    		fii.addElement("UsedYn").addText(Util.getString(UsedYn));
		    		String ValidEndDate=fICoupon.getValidEndDate();
		    		log.debug("ValidEndDate:"+ValidEndDate);
		    		fii.addElement("ValidEndDate").addText(Util.getString(ValidEndDate));
		    		String ValidStartDate=fICoupon.getValidStartDate();
		    		log.debug("ValidStartDate:"+ValidStartDate);
		    		fii.addElement("ValidStartDate").addText(Util.getString(ValidStartDate));
		    	}
		    	returnxml=result.asXML();
		    }
		}catch(Exception e){
			log.error(Util.getERRO(e));
		}
		request.setCharacterEncoding("UTF-8");
	    response.setContentType("text/html;charset=utf-8;");
	    PrintWriter out = response.getWriter();
	    log.debug("xml:"+returnxml);
	    out.print(returnxml);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
